<template>
    <el-select v-model="model" :clearable="false" placeholder="请选择年级" style="width:100%;"  @change="change">
        <el-option  v-for="item in list" :key="item.pId" :label="item.sName" :value="item.pId">
        </el-option>
    </el-select>
</template>

<script>
import { getGradeList, getGradeInfoByUserIdAndSchoolId } from 'api/studentMange';
import { mapGetters } from 'vuex';
export default {
    props: {
        value: {
            type: [String, Number],
            required: true
        },
        isDefaultFill:{
            default:false
        }
    },
    computed: {
        model: {
            get() {
                if (this.list) {
                    if (this.list.some(item => item.pId == this.value)) {
                        let classObj = this.list.find((item) => {
                            return item.pId === this.value;
                        });
                        this.$emit('getGradeName',classObj.sName); 
                        return this.value;
                    } else {
                        this.$emit('input', '');
                        return '';
                    }
                } else {
                    return '';
                }
            },
            set(val) {
                this.$emit('input', val);
            }
        },
        ...mapGetters([
            'schoolId',
            'schoolType',
            'roleType'
        ])
    },
    data() {
        return {
            list: null
        }
    },
    watch: {
        schoolId(val) {
            this.getList();
        }
    },
    created() {
        this.getList();
    },
    methods: {
        getList() {
            this.list = null;
            if (this.schoolId) {
                if(this.roleType =='5'){
                    getGradeInfoByUserIdAndSchoolId(this.schoolId).then( res => {

                        if (res.data.content.length) {
                            let gradeList = res.data.content;
                            if(this.isDefaultFill){//是否要默认回填
                                this.$emit('defaultFillGrade',gradeList[0].gradeId)//默认填充年纪
                            }
                            for (var i = 0;i<gradeList.length;i++) {  
                                for (var j = i+1;j<gradeList.length;j++) {  
                                    if(gradeList[i].gradeId == gradeList[j].gradeId){  
                                        gradeList.splice(j,1);  
                                        j--;  
                                    }  
                                }  
                            }
                            this.list=[];
                            gradeList.map((item,index) => {
                                this.list[index] = {};
                                this.list[index]['pId'] = item.gradeId;
                                this.list[index]['sName'] = item.gradeName;
                            });
                            this.list.unshift({
                                pId: '',
                                sName: '请选择'
                            }); 
                        }              
                    })
                } else {
                    getGradeList(this.schoolId).then(res => {
                        if (res.data.content.length) {
                            this.list = res.data.content;
                            if(this.isDefaultFill){//是否要默认回填
                                this.$emit('defaultFillGrade',this.list[0].pId)//默认填充年纪
                            }
                            this.list.unshift({
                                pId: '',
                                sName: '请选择'
                            });
                        }
                    });
                }
            } else {
                this.$emit('input', '');
            }
        },
        change(val) {
            this.$emit('change', val);
        }
    }
}
</script>

<style>

</style>
